﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Web;

namespace Common
{

    /// <summary>
    /// SendEmail通用类，通过smtp服务器发送邮件
    /// </summary>
    public class EmailHelper
    {

        string EmailName = ConfigurationManager.AppSettings["EmailName"];
        string EmailPass = ConfigurationManager.AppSettings["EmailPass"];
        string Company = ConfigurationManager.AppSettings["company"];

        #region 发送邮件功能
        /// <summary>
        /// 发送邮件功能
        /// </summary>

        /// <param name="user">邮件昵称</param>
        /// <param name="title">邮件标题</param>
        /// <param name="toEmail">邮件地址</param>
        /// <param name="email">邮件内容</param>
        /// <param name="smtpServer">smtp服务器</param>
        public bool SendMessage(string title, string toEmail, string email, string smtpServer, int port)
        {
            try
            {
                SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
                smtp.EnableSsl = false;//smtp服务器是否启用SSL加密
                smtp.Host = smtpServer;//指定 smtp 服务器  
                smtp.Port = port;
                smtp.Credentials = new NetworkCredential(EmailName, EmailPass);
                MailMessage mm = new MailMessage(); //实例化一个邮件类
                mm.Priority = MailPriority.High; //邮件的优先级，分为 Low, Normal, High，通常用 Normal即可       
                mm.From = new MailAddress(EmailName, Company, Encoding.GetEncoding(936));
                mm.CC.Add(new MailAddress(toEmail, "", Encoding.GetEncoding(936)));
                mm.Subject = title; //邮件标题
                mm.SubjectEncoding = Encoding.GetEncoding(936);
                mm.IsBodyHtml = true; //邮件正文是否是HTML格式mm.BodyEncoding = Encoding.GetEncoding(936);
                mm.Body = email;
                smtp.Send(mm);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

    }

}